﻿
using BodaNetCommunications.Nodes;
using BodaNetCommunications.Utilities.Results;

using Opc.Ua;
using Opc.Ua.Client;

namespace BodaNetCommunications.OpcComs
{
    public interface IOpcUaService
    {
        bool IsConnected { get; }

        /// <summary>
        /// 匿名登录
        /// </summary>
        /// <param name="discoveryUrl"></param>
        /// <param name="sessionName"></param>
        /// <param name="sessionTimeout"></param>
        /// <returns></returns>
        BodaResult AnonymousConnect(string discoveryUrl, string sessionName, uint sessionTimeout);

        /// <summary>
        /// 使用用户名和密码登录
        /// </summary>
        /// <param name="useName"></param>
        /// <param name="pwd"></param>
        /// <param name="discoveryUrl"></param>
        /// <param name="sessionName"></param>
        /// <param name="sessionTimeout"></param>
        /// <returns></returns>
        Task<Session> UserNameConnectAsync(string useName, string pwd, string discoveryUrl, string sessionName, uint sessionTimeout);

        /// <summary>
        /// 认证登录
        /// </summary>
        /// <param name="useName"></param>
        /// <param name="pwd"></param>
        /// <param name="discoveryUrl"></param>
        /// <param name="sessionName"></param>
        /// <param name="sessionTimeout"></param>
        /// <returns></returns>
        Task<Session> CertificateConnectAsync(string useName, string pwd, string appName, string appHost, string discoveryUrl, string sessionName, uint sessionTimeout);

        /// <summary>
        /// 1. 读取UA节点数据到List中
        /// List<RequestNode> nodes = new List<RequestNode>();
        /// nodes.Add(new RequestNode { NodeId = "ns=2;s=Tag.PLC_050.TemperatureA.TemperatureNew.EM20300" });
        /// nodes.Add(new RequestNode { NodeId = "ns=2;s=Tag.PLC_050.OtherA.EM1714" });
        /// nodes.Add(new RequestNode { NodeId = "ns=2;s=Tag.PLC_050.OtherA.EM500" });
        /// nodes.Add(new RequestNode { NodeId = "ns=2;s=通道 1.设备 1.标记 2" });
        /// nodes.Add(new RequestNode { NodeId = "ns=2;s=数据类型示例.16 位设备.R 寄存器.WordArray" });
        /// UAAsyncRead(session, nodes, () =>
        /// {
        ///     foreach (var node in nodes)
        ///     {
        ///         Console.WriteLine($"{node.NodeId}:{node.Value}");
        ///     }
        /// });
        /// 
        /// 2. 根据数据类型遍历数据
        /// foreach (var value in results)
        /// {
        ///     if (value.WrappedValue.TypeInfo.ValueRank == -1)//针对值结果
        ///     {
        ///         Console.WriteLine(value);
        ///     }
        ///     else if (value.WrappedValue.TypeInfo.ValueRank == 1)// 针对集合结果
        ///     {
        ///         foreach (var v in (ushort[]) value.WrappedValue.Value)
        ///             Console.WriteLine(v);
        ///     }
        /// }
        /// </summary>
        /// <param name="session"></param>
        /// <param name="nodes"></param>
        /// <param name="inputCallback"></param>
        BodaResult UAReadAsync(List<RequestNode> nodes, Action? inputCallback);

        BodaResult UARead(List<RequestNode> nodes, Action? inputCallback);


        BodaResult UAWrite(List<RequestNode> nodes, Action? inputCallback);

        /// <summary>
        /// 获取所有的节点信息：
        /// 根节点为"ns=0;i=84"
        /// </summary>
        /// <param name="session"></param>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        BodaResult<ReferenceDescriptionCollection> BrowseNodes(string nodeId);

        void DisConnect();
    }
}
